﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace WF.Common
{
    /// <summary>
    /// Trace类，用于记录日志信息
    /// </summary>
    public static class TraceHelper
    {
        /// <summary>
        /// 初始化日志

        /// </summary>
        static TraceHelper()
        {
            InitializeTraceSource(TraceSource);
        }

        /// <summary>
        /// 本应用所有日志类型记为CCHR
        /// </summary>
        public const string TraceSource = "CCHR";

        private static TraceSource m_TraceSource;

        /// <summary>
        /// 获得异常信息
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        [DebuggerStepThrough]
        public static string GetExceptionDetails(Exception ex)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("Exception Information Type[{0}] Message[{1}] ", ex.GetType().Name, ex.Message);
            for (Exception exception = ex.InnerException; exception != null; exception = exception.InnerException)
            {
                builder.AppendFormat("Inner Exception Information Type[{0}] Message[{1}] ", exception.GetType().Name, exception.Message);
            }
            return builder.ToString();
        }

        /// <summary>
        /// 日志初始化

        /// </summary>
        /// <param name="sourceName"></param>
        [DebuggerStepThrough]
        public static void InitializeTraceSource(string sourceName)
        {
            try
            {
                m_TraceSource = new TraceSource(sourceName);
            }
            catch (Exception exception)
            {
                EventLog.WriteEntry("MDCService", "Exception initializing TraceSource: " + exception.Message);
            }
        }

        /// <summary>
        /// 记录开始

        /// </summary>
        /// <param name="p"></param>
        [DebuggerStepThrough]
        public static void TraceBeginMethod(string p)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceEvent(TraceEventType.Start, 1, p);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 记录结束
        /// </summary>
        /// <param name="p"></param>
        [DebuggerStepThrough]
        public static void TraceEndMethod(string p)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceEvent(TraceEventType.Stop, 2, p);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="exmsg"></param>
        [DebuggerStepThrough]
        public static void TraceError(string exmsg)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceEvent(TraceEventType.Error, 0, exmsg);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="exmsg"></param>
        /// <param name="args"></param>
        [DebuggerStepThrough]
        public static void TraceError(string exmsg, params object[] args)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceEvent(TraceEventType.Error, 0, exmsg, args);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="p"></param>
        [DebuggerStepThrough]
        public static void TraceInformation(string p)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceInformation(p);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="p"></param>
        /// <param name="args"></param>
        [DebuggerStepThrough]
        public static void TraceInformation(string p, params object[] args)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceInformation(p, args);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 记录告警日志
        /// </summary>
        /// <param name="p"></param>
        [DebuggerStepThrough]
        public static void TraceWarning(string p)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceEvent(TraceEventType.Warning, 0, p);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 记录告警日志
        /// </summary>
        /// <param name="p"></param>
        /// <param name="args"></param>
        [DebuggerStepThrough]
        public static void TraceWarning(string p, params object[] args)
        {
            if (m_TraceSource != null)
            {
                m_TraceSource.TraceEvent(TraceEventType.Warning, 0, p, args);
                m_TraceSource.Flush();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static TraceSource CCHRTraceSource
        {
            get
            {
                return m_TraceSource;
            }
        }
    }

    public class InfoTextWriter : TextWriter
    {
        public override Encoding Encoding
        {
            get
            {
                return Encoding.Default;
            }
        }

        public override void Write(bool value)
        {
            Write(value.ToString());
        }

        public override void Write(char value)
        {
            Write(value.ToString());
        }

        public override void Write(char[] buffer)
        {
            Write(buffer.ToString());
        }

        public override void Write(decimal value)
        {
            Write(value.ToString());
        }

        public override void Write(double value)
        {
            Write(value.ToString());
        }

        public override void Write(float value)
        {
            Write(value.ToString());
        }

        public override void Write(int value)
        {
            Write(value.ToString());
        }

        public override void Write(long value)
        {
            Write(value.ToString());
        }

        public override void Write(object value)
        {
            Write(value.ToString());
        }

        public override void Write(string value)
        {
            TraceHelper.TraceInformation(value);
        }

        public override void Write(uint value)
        {
            Write(value.ToString());
        }

        public override void Write(ulong value)
        {
            Write(value.ToString());
        }

        public override void Write(string format, object arg0)
        {
            Write(string.Format(format), arg0);
        }

        public override void Write(string format, params object[] arg)
        {
            Write(string.Format(format, arg));
        }

        public override void Write(string format, object arg0, object arg1)
        {
            Write(string.Format(format, arg0, arg1));
        }

        public override void Write(string format, object arg0, object arg1, object arg2)
        {
            Write(string.Format(format, arg0, arg1, arg2));
        }

        public override void WriteLine()
        {
            WriteLine("");
        }

        public override void WriteLine(bool value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(char value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(char[] buffer)
        {
            WriteLine(buffer.ToString());
        }

        public override void WriteLine(decimal value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(double value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(float value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(int value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(long value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(object value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(string value)
        {
            TraceHelper.TraceInformation(value);
        }

        public override void WriteLine(uint value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(ulong value)
        {
            WriteLine(value.ToString());
        }

        public override void WriteLine(string format, object arg0)
        {
            WriteLine(string.Format(format, arg0));
        }

        public override void WriteLine(string format, params object[] arg)
        {
            WriteLine(string.Format(format, arg));
        }

        public override void WriteLine(string format, object arg0, object arg1)
        {
            WriteLine(string.Format(format, arg0, arg1));
        }

        public override void WriteLine(string format, object arg0, object arg1, object arg2)
        {
            WriteLine(string.Format(format, arg0, arg1, arg2));
        }
    }
}
